// 创建cookie
// key：键名 val：键值 day：过期时间 path：存储路径
function setCookie(key, val, day, path = "/") {
    if (day) { //如果传入过期时间
        var date = new Date();
        date.setDate(date.getDate() + day);
        document.cookie = `${key}=${encodeURIComponent(val)};expires=${date.toUTCString()};path=/`;
    } else {
        document.cookie = `${key}=${encodeURIComponent(val)};path=/`;
    }
}

// 获取cookie
function getCookie(key) {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; "); //将cookie通过"; "拆分成数组
        for (var i = 0; i < list.length; i++) {
            var name = list[i].split("=")[0]; //通过等号拆分，把键名给nane
            var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
            if (key === name) { //如果键值和name相等，直接返回键值
                return decodeURIComponent(val);
            }
        }
    }
    return "";

    //获取全部
    // var cookie = document.cookie;
    // var obj = {};
    // if (cookie) {
    //     var list = cookie.split("; "); //将cookie通过"; "拆分成数组
    //     for (var i = 0; i < list.length; i++) {
    //         var name = list[i].split("=")[0]; //通过等号拆分，把键名给nane
    //         var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
    //         obj[name] = val;
    //     }
    // }
    // console.log(obj);
}

//删除cookie
function delCookie(key) {
    setCookie(key, "", -1);
}

//清除cookie
function clerCookie() {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; "); //将cookie通过"; "拆分成数组
        for (var i = 0; i < list.length; i++) {
            var name = list[i].split("=")[0]; //通过等号拆分，把键名给nane
            var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
            setCookie(name, "", -1);
        }
    }
}

// 获取用户名集合
function userList() {
    var cookie = document.cookie;
    var arr = [];
    if (cookie) {
        var list = cookie.split("; "); //将cookie通过"; "拆分成数组
        for (var i = 0; i < list.length; i++) {
            var key = list[i].split("=")[0]; //通过等号拆分，把键名给nane
            var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
            if(key.startsWith("user_")) {
                arr.push(decodeURIComponent(val));
            }
        }
    }
    return arr;
}

// 获取手机号名集合
function phoneList() {
    var cookie = document.cookie;
    var arr = [];
    if (cookie) {
        var list = cookie.split("; "); //将cookie通过"; "拆分成数组
        for (var i = 0; i < list.length; i++) {
            var key = list[i].split("=")[0]; //通过等号拆分，把键名给nane
            var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
            if(key.startsWith("phone_")) {
                arr.push(decodeURIComponent(val));
            }
        }
    }
    return arr;
}

// 获取邮箱名集合
function emailList() {
    var cookie = document.cookie;
    var arr = [];
    if (cookie) {
        var list = cookie.split("; "); //将cookie通过"; "拆分成数组
        for (var i = 0; i < list.length; i++) {
            var key = list[i].split("=")[0]; //通过等号拆分，把键名给nane
            var val = list[i].split("=")[1]; //通过等号拆分，把键值给val
            if(key.startsWith("email_")) {
                arr.push(decodeURIComponent(val));
            }
        }
    }
    return arr;
}